package com.geezdata.cps.sdk3rd.sms.provider;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.geezdata.cps.sdk3rd.sms.request.BalanceRequest;
import com.geezdata.cps.sdk3rd.sms.request.SmsRequest;
import com.geezdata.cps.sdk3rd.sms.response.SmsResult;

/**
 * 253云通讯(YTX)PaaS短信 
 * @author jghu
 *
 */
public class YTXProvider extends BaseProvider {
	
	private static final String DEFAULT_STRING_OF_REPORT = "true";

	@Override
	protected boolean usePost() {
		return true;
	}

	@Override
	protected String prepareSendUrl(SmsRequest request) {
		return request.getDownlink();
	}

	@Override
	protected Map<String, Object> prepareSendParams(SmsRequest request) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("account", request.getUsername());
		params.put("password", request.getPassword());
		params.put("msg", request.getContent());
		params.put("phone", request.getMobiles());
		params.put("report",DEFAULT_STRING_OF_REPORT);
		params.put("uid", request.getSubId());
		return params;
	}

	@Override
	protected SmsResult parseSendResponse(String response) {
		if (StringUtils.isEmpty(response)) {
			return null;
		}
		
		SmsResult result = new SmsResult();
		JSONObject jsonObject = JSONObject.parseObject(response);
		if(jsonObject.getString("msgId")!= null) {
			result.setBatchSn(jsonObject.getString("msgId"));
		}
		
		if(jsonObject.getString("code")!=null) {
			result.setResponseCode(jsonObject.getString("code"));
		}
		
		return result;
	}

	@Override
	protected String prepareQueryUrl(BalanceRequest request) {
		return request.getQueryurl();
	}

	@Override
	protected Integer parseQueryResponse(String response) {
		if (StringUtils.isEmpty(response)) {
			return null;
		}
		
		Integer balance = 0;
		JSONObject jsonObject = JSONObject.parseObject(response);
		if(jsonObject.getString("code").equals("0")) {
			balance = Integer.valueOf(jsonObject.getString("balance"));
		}

		return balance;
	}

	@Override
	protected Map<String, Object> prepareQueryParams(BalanceRequest request) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("account", request.getUsername());
		params.put("password", request.getPassword());
		return params;
	}

}
